package cn.eeepay.boss.action.api;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.eeepay.boss.system.DataSource;
import cn.eeepay.framework.model.AgentOperLog;
import cn.eeepay.framework.model.Result;
import cn.eeepay.framework.model.WarningEvents;
import cn.eeepay.framework.service.AgentOperLogService;
import cn.eeepay.framework.service.WarningEventsService;
import cn.eeepay.framework.util.Constants;
import cn.eeepay.framework.util.StringUtil;

@RestController
@RequestMapping("/managerSmsApi")
public class ManagerSmsApiAction {

	private static final Logger log = LoggerFactory.getLogger(ManagerSmsApiAction.class);
	@Resource
    private WarningEventsService warningEventsService;
	@Resource
	private AgentOperLogService agentOperLogService;
	/**
     * 添加短信预警
     * @param params
     * @return
     */
    @RequestMapping(value = "/addWarningEvents",method = RequestMethod.POST)
    @DataSource(Constants.DATA_SOURCE_MASTER)
    public Result addWarningEvents(@RequestParam Map<String, Object> params){

        log.info("添加短信预警事件start,param:{}", params.toString());
        Result result = new Result();
        try{
        	WarningEvents we=new WarningEvents();
        	we.setEventNumber(String.valueOf(params.get("eventId")));
        	we.setMessage(String.valueOf(params.get("content")));
        	we.setStatus(1);      
        	we.setSmsCount(Integer.valueOf(params.get("smsCount").toString()));
        	
        	int num=warningEventsService.insertSMSWarningEvents(we);
        	//发送预警短信
            if(num>0){
            	result.setStatus(true);
	        	result.setCode(200);
	        	result.setMsg("添加成功");
            }else{
	        	result.setCode(300);
	        	result.setMsg("添加失败");
            }        	
        }catch(Exception e){
        	log.error("查询出款订单异常", e);
            result.setCode(400);
            result.setMsg("添加异常");
            return result;
        }
        log.info("添加短信预警事件end");
        return result;
    }
    
    /**
     * 记录自动切换日志记录
     * @param params
     * @return
     */
    @RequestMapping(value = "/addSelfChangelog",method = RequestMethod.POST)
    @DataSource(Constants.DATA_SOURCE_MASTER)
    public Result addSelfChangelog(@RequestParam Map<String, Object> params){

        log.info("记录自动切换日志记录start,param:{}", params.toString());
        Result result = new Result();
        try{
        	String request_params = (String) params.get("requestParams");
        	if(request_params != null && !StringUtil.isBlank(request_params)){
        		int num= agentOperLogService.insertSms(request_params);
	        	//发送预警短信
	            if(num>0){
	            	result.setStatus(true);
		        	result.setCode(200);
		        	result.setMsg("添加成功");
	            }else{
		        	result.setCode(300);
		        	result.setMsg("添加失败");
	            }  
        	}else{
        		result.setCode(300);
	        	result.setMsg("参数为空");
        	}  	
        }catch(Exception e){
        	log.error("记录自动切换日志记录异常", e);
            result.setCode(400);
            result.setMsg("添加异常");
            return result;
        }
        log.info("记录自动切换日志记录end");
        return result;
    }
}
